package com.t.e.leaksdemo;

import android.app.ActivityManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.lang.ref.*;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MemoryInfo";
    public static String mainstr;
    public WeakReference<String> weakRef ;
    public TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.tv_main_activity);

        displayBriefMemory();

        findViewById(R.id.btn_test_aty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, LeaksActivity.class);
                startActivity(i);
            }
        });

        findViewById(R.id.btn_test_frgmt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.addToBackStack("leak frgmt").replace(R.id.container, new LeakFrgmt());
                ft.commit();
            }
        });
        findViewById(R.id.btn_innerclass_leaks).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, InnerClassLeaksActivity.class);
                startActivity(i);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);

        switch (level){
            case TRIM_MEMORY_RUNNING_MODERATE:
                break;
            case TRIM_MEMORY_UI_HIDDEN://这里释放 ui资源
                break;
            default:break;
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    private void displayBriefMemory() {
        final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(info);
        tv.setText("meminfo 系统剩余内存:" + (info.availMem >> 10) + "k" + "\n");
        tv.append("系统是否处于低内存运行：" + info.lowMemory + "\n");
        tv.append( "当系统剩余内存低于" + (info.threshold >> 10) + "k" + "时就看成低内存运行" + "\n");
    }
}
